package com.app.qbake.module.mine;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aigestudio.wheelpicker.utils.WheelPickerFactory;
import com.aigestudio.wheelpicker.widget.IWheelVo;
import com.devilist.recyclerwheelpicker.TripleWheelPicker;
import com.devilist.recyclerwheelpicker.dialog.WheelPicker;
import com.wind.base.bean.User;
import com.wind.base.mvp.view.BaseLoadingFragment;
import com.wind.base.request.BaseRequest;
import com.wind.base.utils.ViewInflater;
import com.wind.coder.annotations.Api;
import com.wind.coder.annotations.Heros;
import com.wind.coder.annotations.Param;
import com.wind.data.mine.EditInfoResponse;
import com.wind.data.mine.UserInfoRequest;
import com.wind.data.mine.UserInfoResponse;
import com.app.qbake.R;
import com.app.qbake.module.mine.activity.AddressActivity;
import com.app.qbake.module.mine.di.UserInfoComponent;
import com.app.qbake.module.mine.layout.AvatarChangeLayout;
import com.app.qbake.module.mine.layout.EditInfoLayout;
import com.app.qbake.module.mine.mvp.UserInfoView;
import com.app.qbake.module.mine.presenter.UserInfoPresenter;
import com.app.qbake.util.AppDialogHelper;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

@Heros(
        param = @Param(
                viewCanonicalName = "com.app.qbake.module.mine.mvp.UserInfoView",
                responseCanonicalName = "com.wind.data.mine.UserInfoResponse",
                requestCanonicalName = "com.wind.data.mine.UserInfoRequest"

        ),
        api = @Api(httpMethod = Api.HttpMethod.GET, url = "user/info")
)
public class UserInfoFragment extends BaseLoadingFragment<UserInfoView, UserInfoPresenter, UserInfoComponent, UserInfoResponse>
        implements UserInfoView, AvatarChangeLayout.OnUploadPhotoListener, EditInfoLayout.OnEditListener {


    AvatarChangeLayout mAvatarChangeLayout;
    EditInfoLayout mEditInfoLayout;

    private String mAvatarUrl;
    @BindView(R.id.iv_avatar)
    ImageView iv_avatar;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_birthday)
    TextView tv_birthday;
    @BindView(R.id.tv_gender)
    TextView tv_gender;
    @BindView(R.id.tv_abode)
    TextView tv_abode;
    @BindView(R.id.tv_introduce)
    TextView tv_introduce;
    @Inject
    UserInfoPresenter presenter;

    private User mLoginUser;

    @Override
    public UserInfoPresenter createPresenter() {
        return presenter;
    }

    @Override
    protected BaseRequest buildLoadingRequest() {
        UserInfoRequest request = new UserInfoRequest();
        return request;
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.fragment_userinfo;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mAvatarChangeLayout = new AvatarChangeLayout(getActivity());
        mAvatarChangeLayout.setOnUploadPhotoListener(this);
        mEditInfoLayout = new EditInfoLayout(getActivity(), this);

    }

    @Override
    public void showPage(UserInfoResponse reponse) {
        mLoginUser = reponse.getData().getUserInfo();
        inflateInfo(reponse.getData().getUserInfo());
        mLayoutManager.showContent();
    }

    @OnClick({R.id.rl_avatar, R.id.rl_name, R.id.rl_birthday, R.id.rl_gender, R.id.rl_abode,
            R.id.rl_introduce, R.id.rl_address})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.rl_avatar:
                mAvatarChangeLayout.showAvatarSelectDialog();
                break;
            case R.id.rl_name:
                String name = tv_name.getText().toString();
                AppDialogHelper.showEditTextDialog(getActivity(), 1, name, new AppDialogHelper.DialogInputCallback() {
                    @Override
                    public void onDialogInputReturn(String input) {
                        User curUser = buildCurrentUser();
                        curUser.setUserName(input);
                        showOpLoadingIndicator();
                        mEditInfoLayout.edit(curUser);
                    }
                });
                break;
            case R.id.rl_birthday:
                WheelPickerFactory.showWheelBirthdayPicker(tv_birthday, new WheelPickerFactory.OnWheelClickListener() {
                    @Override
                    public void onResult(View v, IWheelVo[] result, int[] indexs, String[] unit) {
                        String year = result[0].getLabel();
                        String month = result[1].getLabel();
                        String day = result[2].getLabel();
                        User curUser = buildCurrentUser();
                        curUser.setBirthday(year + "-" + month + "-" + day);
                        showOpLoadingIndicator();
                        mEditInfoLayout.edit(curUser);
                    }
                }, 1993, 1, 1);
                break;
            case R.id.rl_gender:

                WheelPickerFactory.showWheelAPicker(tv_gender, new WheelPickerFactory.OnWheelClickListener() {
                    @Override
                    public void onResult(View v, IWheelVo[] result, int[] indexs, String[] unit) {
                        User curUser = buildCurrentUser();
                        String gender = result[0].getLabel();
                        int sex;
                        if ("男".equals(gender)) {
                            sex = User.CODE_GENDER_MAN;
                        } else if ("女".equals(gender)) {
                            sex = User.CODE_GENDER_WOMAN;
                        } else {
                            sex = User.CODE_GENDER_UNKNOWN;
                        }

                        curUser.setSex(sex);
                        showOpLoadingIndicator();
                        mEditInfoLayout.edit(curUser);
                    }
                }, R.xml.wheel_gender, "");
                break;

            case R.id.rl_abode:

                WheelPicker picker = TripleWheelPicker.instance()
                        .setGravity(Gravity.BOTTOM)
                        .setResource(R.raw.picker_location_3)
                        .setPickerListener(new WheelPicker.OnPickerListener() {
                            @Override
                            public void onPickResult(String tag, String... result) {
                                User curUser = buildCurrentUser();
                              /*  String province=result[0].getLabel();
                                String city=result[1].getLabel();
                                String abode;
                                if (province.equals(city)){
                                    abode=province;
                                }else {
                                    abode=province+"-"+city;
                                }*/
                                String province;
                                String city;
                                String area;
                                String abode;

                                province = result[0];
                                city = result[1];
                                area = result[2];
                                if (TextUtils.isEmpty(area)){
                                    abode = province + "-" + city;
                                }else {
                                    abode = province + "-" + city + "-" + area;
                                }


                                curUser.setAbode(abode);
                                showOpLoadingIndicator();
                                mEditInfoLayout.edit(curUser);
                            }
                        })
                        .build();
                picker.show(getChildFragmentManager());
               /* WheelPickerFactory.showWheelAAPicker(tv_gender, new WheelPickerFactory.OnWheelClickListener() {
                    @Override
                    public void onResult(View v, IWheelVo[] result, int[] indexs, String[] unit) {
                        User curUser=buildCurrentUser();
                        String province=result[0].getLabel();
                        String city=result[1].getLabel();
                        String abode;
                        if (province.equals(city)){
                            abode=province;
                        }else {
                            abode=province+"-"+city;
                        }

                        curUser.setAbode(abode);
                        showOpLoadingIndicator();
                        mEditInfoLayout.edit(curUser);
                    }
                },R.xml.wheel_location,"","");*/
                break;
            case R.id.rl_introduce:
                String introduce = tv_introduce.getText().toString();
                AppDialogHelper.showEditTextDialog(getActivity(), 4, introduce, new AppDialogHelper.DialogInputCallback() {
                    @Override
                    public void onDialogInputReturn(String input) {

                        User curUser = buildCurrentUser();
                        curUser.setIntroduce(input);
                        showOpLoadingIndicator();
                        mEditInfoLayout.edit(curUser);
                    }
                });
                break;
            case R.id.rl_address:
                AddressActivity.start(getActivity());
                break;
        }
    }


    @Override
    public void onUploadSuccess(String url) {
        User curUser = buildCurrentUser();
        curUser.setAvatar(url);
        mEditInfoLayout.edit(curUser);
    }

    @Override
    public void onEditReturn(EditInfoResponse response) {
        mLoginUser = response.getData();
        inflateInfo(response.getData());
    }

    private void inflateInfo(User user) {
        mAvatarUrl = user.getAvatar();
        ViewInflater.inflateImage(iv_avatar, user.getAvatar());
        ViewInflater.inflateText(tv_name, user.getUserName());
        ViewInflater.inflateText(tv_birthday, user.getBirthday());
        ViewInflater.inflateText(tv_abode, user.getAbode());
        ViewInflater.inflateText(tv_introduce, user.getIntroduce());
        int gender = user.getSex();
        String genderS = "";
        switch (gender) {
            case User.CODE_GENDER_MAN:
                genderS = "男";
                break;
            case User.CODE_GENDER_WOMAN:
                genderS = "女";
                break;
            case User.CODE_GENDER_UNKNOWN:
                genderS = "未知";
                break;
        }
        ViewInflater.inflateText(tv_gender, genderS);
    }


    private User buildCurrentUser() {
       /* User user = new User();
        if (TextUtils.isEmpty(url)) {
            user.setAvatar(mAvatarUrl);
        } else {
            user.setAvatar(url);
        }
        user.setBirthday(getText(tv_birthday));
        String genderS = getText(tv_gender);
        int gender;
        if ("男".equals(genderS)) {
            gender = User.CODE_GENDER_MAN;
        } else if ("女".equals(genderS)) {
            gender = User.CODE_GENDER_WOMAN;
        } else {
            gender = User.CODE_GENDER_UNKNOWN;
        }
        user.setSex(gender);

        user.setUserName(getText(tv_name));

        user.setAbode(getText(tv_abode));

        user.setIntroduce(getText(tv_introduce));*/

        User user = new User();
        user.setAvatar(mLoginUser.getAvatar());
        user.setIntroduce(mLoginUser.getIntroduce());
        user.setBirthday(mLoginUser.getBirthday());
        user.setSex(mLoginUser.getSex());
        user.setUserName(mLoginUser.getUserName());
        user.setAbode(mLoginUser.getAbode());

        return user;
    }



}
